﻿// JavaScript Document
var NB_styles = {
						navbar:"Header_navBar",
						navbar_head:"Header_navBar_head",
						navbar_body:"Header_navBar_body",
						navbar_foot:"Header_navBar_foot",
						li_normal:"normal",
						li_left:"left",
						li_content:"content",
						li_right:"right",
						li_active:"active",
						li_arrow:"arrow"
					};

function NavBar(arr,styles,rSpace){
	try{
		
		this.arr = arr;
		this.styles = styles;
		
		var oNavbar;
		var oNavbar_head;
		var oNavbar_body;
		var oNavbar_foot;
		var oNavbar_ul;
		var oNavbar_li;
		var oNavbar_a;
		var oNavbar_li_left;
		var oNavbar_li_content;
		var oNavbar_li_right;
		var fWidth = 0;
		var navbar_length = 0;
		var lWidth = 0;
		var menuarr = [];
		var oNavbarmenu;
		
		this.createToObj = function(obj){
			oNavbar = document.createElement('div');
			oNavbar.className = this.styles.navbar;
			oNavbar_head = document.createElement('div'); 
			oNavbar_head.className = this.styles.navbar_head;
			oNavbar_body = document.createElement('div'); 
			oNavbar_body.className = this.styles.navbar_body;
			oNavbar_foot = document.createElement('div'); 
			oNavbar_foot.className = this.styles.navbar_foot;
			oNavbar_ul = document.createElement('ul');
			for(var i = 0; i < this.arr.length; i++){
				oNavbar_li = document.createElement('li');
				oNavbar_li.id = this.arr[i].id;
				oNavbar_li.className = this.styles.li_normal;
				oNavbar_li_left = document.createElement('div');
				oNavbar_li_left.className = this.styles.li_left;
				oNavbar_li_content = document.createElement('div');
				oNavbar_li_content.className = this.styles.li_content;
				oNavbar_li_right = document.createElement('div');
				oNavbar_li_right.className = this.styles.li_right;
				
				oNavbar_a = document.createElement('a');
				oNavbar_a.innerHTML = this.arr[i].name;
				oNavbar_a.href = this.arr[i].href == "" ? "javascript:void(0)" : this.arr[i].href;
				oNavbar_a.title = this.arr[i].title;
				
				oNavbar_li_content.appendChild(oNavbar_a);
				oNavbar_li.appendChild(oNavbar_li_left);
				oNavbar_li.appendChild(oNavbar_li_content);
				oNavbar_li.appendChild(oNavbar_li_right);
				
				oNavbar_ul.appendChild(oNavbar_li);
			}
			oNavbar_body.appendChild(oNavbar_ul);
			oNavbar.appendChild(oNavbar_head);
			oNavbar.appendChild(oNavbar_body);
			oNavbar.appendChild(oNavbar_foot);
			
			obj.appendChild(oNavbar);
			
			this.createMenu();
		}
		this.getFactWidth = function(){
			var w = 0;
			for(var i = 0; i < navbar_length; i++){
				w += oNavbar_ul.getElementsByTagName("li")[i].offsetWidth;
			}
			return w;
		}
		this.createMenu = function(){
			if(oNavbarmenu != null){
				document.body.removeChild(oNavbarmenu.returnObject());
				oNavbarmenu = null;
			}
			
			menuarr = [];
			navbar_length = this.arr.length;
			lWidth = $('autoFit').offsetWidth - rSpace;
			fWidth = this.getFactWidth();
			if(fWidth > lWidth){
				while(true){
					if(fWidth > lWidth){
						oNavbar_ul.removeChild(oNavbar_ul.getElementsByTagName("li")[navbar_length - 1]);
						menuarr.push(this.arr[navbar_length - 1]);
						navbar_length = oNavbar_ul.getElementsByTagName("li").length;
						fWidth = this.getFactWidth();
					}else{
						break;
					}
				}
				menuarr = menuarr.reverse();           
				var oArrow = document.createElement('li');
				var oArrow_a = document.createElement('a');
				oArrow.className = this.styles.li_arrow;
				oArrow.appendChild(oArrow_a);
				oNavbar_ul.appendChild(oArrow);
				oNavbarmenu = new SysMenu(menuarr,menu_styles);
				oNavbarmenu.createToObj(document.body);
				oArrow.onmouseover = function(){
					oNavbarmenu.show(this)
				}
			}
		}
		this.resize = function(){
			lWidth = $('autoFit').offsetWidth - rSpace;
			fWidth = oNavbar_ul.offsetWidth;
			
			if(fWidth > lWidth){
				if(oNavbarmenu != null){
					navbar_length = oNavbar_ul.getElementsByTagName("li").length;
					oNavbar_ul.removeChild(oNavbar_ul.getElementsByTagName("li")[navbar_length - 1]);
				}
				
				for(var i = 0; i < menuarr.length; i++){
					oNavbar_li = document.createElement('li');
					oNavbar_li.id = menuarr[i].id;
					oNavbar_li.className = this.styles.li_normal;
					oNavbar_li_left = document.createElement('div');
					oNavbar_li_left.className = this.styles.li_left;
					oNavbar_li_content = document.createElement('div');
					oNavbar_li_content.className = this.styles.li_content;
					oNavbar_li_right = document.createElement('div');
					oNavbar_li_right.className = this.styles.li_right;
					
					oNavbar_a = document.createElement('a');
					oNavbar_a.innerHTML = menuarr[i].name;
					oNavbar_a.href = menuarr[i].href == "" ? "javascript:void(0)" : menuarr[i].href;
					oNavbar_a.title = menuarr[i].title;
					
					oNavbar_li_content.appendChild(oNavbar_a);
					oNavbar_li.appendChild(oNavbar_li_left);
					oNavbar_li.appendChild(oNavbar_li_content);
					oNavbar_li.appendChild(oNavbar_li_right);
					
					oNavbar_ul.appendChild(oNavbar_li);
				}
				this.createMenu();
			}
		}
	}catch(e){}
}